<?php
class Pages extends Zend_Db_Table_Abstract
{
	protected $_name = 'pages';

	public function fetchAllByLanguage($languageId = 0)
	{
		$select = $this->select();
		$select->from($this->_name);
		$select->setIntegrityCheck(false);
		$select->join("pagesL10N", "pagesL10N.pageId = {$this->_name}.id");

		if(is_numeric($languageId) && $languageId != 0) {
			$select->where("pagesL10N.languageId = '{$languageId}'");
		}

		$this->getAdapter()->setFetchMode(Zend_Db::FETCH_OBJ);
		return $this->getAdapter()->fetchAll($select);
	}

	public function generateTree($languageId = 0)
	{
		$options = array();
		$references = array();

		$pages = $this->fetchAllByLanguage($languageId);

		foreach($pages as $page) {
			$ref = &$references[$page->pageId];
			$ref['parentId'] = $page->parentId;
			$ref['name'][$page->languageId] = $page->name;

			if($page->parentId == 0) {
				$options[$page->pageId] = &$ref;
			} else {
				$references[$page->parentId]['children'][$page->pageId] = &$ref;
			}
		}

		return $options;
	}
}